<template>
  <div>
    <el-backtop></el-backtop>
  <el-collapse v-model="activeName" accordion>
    <el-collapse-item
    v-for="(diary,did) in diaryList"
    :key="diary.did"
    :title="diary.title"
    :name="diary.did">
      <div>{{diary.content}}</div>
      <p>{{diary.editTime}}
      <el-button type="primary" size="mini" round plain
      @click="gotoDrafts(diary.did)">恢复至草稿箱</el-button>
      <el-button type="danger" size="mini" round plain
      @click="deleteDiary(diary.did)">彻底删除</el-button></p>
    </el-collapse-item>
  </el-collapse>
    </div>
</template>

<script>
    import axios from 'axios'
    import Login from '../Jun/Login.vue'

   export default {
     mounted(){
       var _this = this;
       _this.uid = localStorage.getItem('user_id');
       var token = localStorage.getItem("token");
       axios.post('http://oneday.vaiwan.com/diary/show',{
           uid:_this.uid,
           state:3
       },{headers:{
          token:`${token}`},
          }).then(function(response){
         _this.diaryList = response.data.data;
       }).catch(function(error){
         alert("请重新登录！");
         _this.$router.push("/");
       });
     },
     data() {
      return {
        activeName: '',
        uid:'',
        diaryList:[{
          did:'',
          title:'',
          content:'',
          editTime:'',
          state:''
        }],

        /*测试用数组
        diaryList:[{
          did:'0',
          editTime:'2022-04-12',
          title:'a',
          content:'aa',
          state:3
        },{
          did:'1',
          editTime:'2022-04-11',
          title:'b',
          content:'aad',
          state:3
        },{
          did:'2',
          editTime:'2022-04-10',
          title:'av',
          content:'aaaa',
          state:3
        }]
        */
       };
     },

     methods:{
      gotoDrafts(did){
        var _this = this;
        var token = localStorage.getItem("token");
        //向后端发送请求，进入草稿箱，state为1
          axios.post('http://oneday.vaiwan.com/diary/state', {
            state:1,
            did:did
              },{headers:{
          token:`${token}`},
          })
              .then(function(response){
                //成功时返回response数据
                /*
                did:""
                state:"1"
                eiditTime:""
                */
               alert("恢复成功！");
               _this.$router.replace("/drafts");
              })
              .catch(function(error){
                alert("请重新登录！");
                _this.$router.push("/");
              });
      },
      deleteDiary(did){
        var _this = this;
        var token = localStorage.getItem("token");
        //向后端发送请求，彻底删除，不需要发送state
          axios.post('http://oneday.vaiwan.com/diary/delete', {
            did:did
              },{headers:{
          token:`${token}`},
          }).then(function(response){
                //成功时返回response数据
                /*
                did:""
                state:"1"
                eiditTime:""
                */
               alert("删除成功！");
               _this.$router.replace("/recycle");
              })
              .catch(function(error){
                alert("请重新登录！");
                _this.$router.push("/");
              });
      }
    }
  }
 </script>
